package com.mojian.controller.resource;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mojian.common.Result;
import com.mojian.entity.SysResource;
import com.mojian.service.ResourceService;
import com.mojian.vo.resource.ResourceDownloadVo;
import com.mojian.vo.resource.SysResourceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/resource")
@RequiredArgsConstructor
@Api(tags = "门户-资源管理")
public class ResourceController {

    private final ResourceService resourceService;

    @GetMapping("/list")
    @Operation(description = "资源列表")
    public Result<Page<SysResourceVo>> getResourceList(SysResource sysResource) {
        Page<SysResourceVo> result = resourceService.getResourceList(sysResource);
        // 日志检查类型
        if (result != null && result.getRecords() != null) {
            for (SysResourceVo vo : result.getRecords()) {
                System.out.println("列表资源[" + vo.getId() + ":" + vo.getName() + "] 类型值:" + vo.getType());
            }
        }
        return Result.success(result);
    }

    @SaCheckLogin
    @PostMapping("/add")
    @Operation(description = "上传资源")
    public Result<Void> add(@RequestBody SysResource sysResource) {
        resourceService.add(sysResource);
        return Result.success();
    }

    @GetMapping("/verify")
    @Operation(description = "校验验证码")
    public Result<SysResource> verify(String code, Long id) {
        return Result.success(resourceService.verify(code, id));
    }
    
    @GetMapping("/{id}")
    @Operation(description = "获取资源详情")
    public Result<SysResourceVo> getResourceDetail(@PathVariable Long id) {
        SysResourceVo result = resourceService.getResourceDetail(id);
        // 日志检查类型
        if (result != null) {
            System.out.println("控制器返回资源详情[" + result.getId() + ":" + result.getName() + 
                               "] 类型值:" + result.getType() + 
                               ", 积分:" + result.getPoints() + 
                               ", 网盘地址:" + result.getPanPath() + 
                               ", 提取码:" + result.getPanCode());
        }
        return Result.success(result);
    }
    
    @SaCheckLogin
    @GetMapping("/check-permission/{id}")
    @Operation(description = "检查下载权限")
    public Result<Boolean> checkDownloadPermission(@PathVariable Long id) {
        return Result.success(resourceService.checkDownloadPermission(id));
    }
    
    @SaCheckLogin
    @PostMapping("/exchange/{id}")
    @Operation(description = "兑换资源")
    public Result<Boolean> exchangeResource(@PathVariable Long id) {
        return Result.success(resourceService.exchangeResource(id));
    }
    
    @SaCheckLogin
    @GetMapping("/download/{id}")
    @Operation(description = "下载资源")
    public Result<String> downloadResource(@PathVariable Long id) {
        return Result.success(resourceService.getDownloadUrl(id));
    }
    
    @SaCheckLogin
    @GetMapping("/my-resources")
    @Operation(description = "我的上传资源")
    public Result<Page<SysResourceVo>> getMyResources(
            @ApiParam(value = "页码", required = true) @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "资源名称") @RequestParam(required = false) String name) {
        return Result.success(resourceService.getMyResources(pageNum, pageSize, name));
    }
    
    @SaCheckLogin
    @GetMapping("/my-downloads")
    @Operation(description = "我的下载资源")
    public Result<Page<ResourceDownloadVo>> getMyDownloads(
            @ApiParam(value = "页码", required = true) @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(resourceService.getMyDownloads(pageNum, pageSize));
    }
    
    @SaCheckLogin
    @DeleteMapping("/{id}")
    @Operation(description = "删除资源")
    public Result<Boolean> deleteResource(@PathVariable Long id) {
        return Result.success(resourceService.deleteResource(id));
    }
}
